package csl.bluetooth.base;

import csl.bluetooth.tools.utils.CaptchaGenerator;
import csl.bluetooth.tools.utils.MD5Util;
import csl.bluetooth.tools.conn.ConnectTool;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * @author 陈少磊
 * @version 1.0.0
 * @ClassName Login.java
 * @Description TODO 用户登陆
 * @createTime 2021年03月24日 22:46:00
 */
public class Login extends JFrame implements ActionListener {

    private Connection conn;
    private String verifyCode;
    private Boolean ifVerify = false;

    public static void main(String[] args) {
        try {
            //设置本属性将改变窗口边框样式定义
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            UIManager.put("RootPane.setupButtonVisible", true);
        }
        catch(Exception e) {
            //TODO exception
        }
        new Login();
    }

    private final JPanel jp = new JPanel();
    private final JLabel ul = new JLabel("用户名：");
    private final JLabel pl = new JLabel("密  码：");
    private final JLabel vl = new JLabel("验证码：");
    private final JLabel ts = new JLabel("");
    private final JTextField uname = new JTextField();
    private final JPasswordField pword = new JPasswordField();
    private final JTextField verify = new JTextField();
    private final JButton login = new JButton("登陆");
    private final JButton getIcon = new JButton("更新验证码");

    public Login() {
        addListener();
        initialFrame();
        getVerify();
        this.ts.setText("验证码为:"+this.verifyCode);
    }
    private void initialFrame() {
        Font font = new Font("宋体",Font.ITALIC,20);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("RFID标签读写系统");
        Image img = new ImageIcon("resource/RFID-icon.png").getImage();
        setIconImage(img);
        jp.setBackground(Color.WHITE);
        jp.setLayout(null);

        ul.setBounds(280, 30, 60, 30);
        ul.setFont(new Font("宋体",Font.ITALIC,15));
        jp.add(ul);
        uname.setBounds(340, 30, 240, 30);
        uname.setFont(new Font("宋体",Font.ITALIC,15));
        jp.add(uname);

        pl.setBounds(280, 80, 60, 30);
        pl.setFont(new Font("宋体",Font.ITALIC,14));
        pword.setBounds(340, 80, 240, 30);
        pword.setFont(new Font("宋体",Font.ITALIC,15));
        jp.add(pl);
        jp.add(pword);

        vl.setBounds(280,130,60,30);
        vl.setFont(new Font("宋体",Font.ITALIC,15));
        verify.setBounds(340, 130, 240, 30);
        verify.setFont(new Font("宋体",Font.ITALIC,14));
        jp.add(verify);
        jp.add(vl);

        ts.setBounds(340, 160, 350, 50);
        jp.add(ts);
        ts.setFont(font);
        login.setBounds(305, 220, 150, 30);
        getIcon.setBounds(465,220,150,30);
        jp.add(login);
        jp.add(getIcon);
        getIcon.setFont(font);
        login.setFont(font);
        add(jp);
        setResizable(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
        setBounds(width / 4, height / 4, width / 2, height / 3);
        setVisible(true);
        uname.requestFocus(true);
        getContentPane().add(jp);
        jp.getRootPane().setDefaultButton(login);
    }
    private void addListener() {
        this.login.addActionListener(this);
        this.getIcon.addActionListener(this);
        this.uname.addActionListener(this);
        this.pword.addActionListener(this);
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if ("更新验证码".equals(actionCommand)){
            this.ts.setText("正在为您努力加载，请稍等......");
            getVerify();
            this.ts.setText("验证码为:"+this.verifyCode);
        }else if("登陆".equals(actionCommand)){
            this.ts.setText("正在为您努力加载，请稍等......");
            String username = uname.getText().trim();
            String password = pword.getText().trim();
            String verifyString = verify.getText();
            if (!"".equals(username) && !"".equals(password)){
                if(ifVerify && !"".equals(verifyString) && verifyString.equals(this.verifyCode)){
                    //开始登陆验证
                    try {
                        conn = new ConnectTool().getConn();
                        String sql = "select * from ias_user where userCount=? and userPassward=?";
                        PreparedStatement ps = conn.prepareStatement(sql);
                        ps.setString(1,username);
                        ps.setString(2, MD5Util.MD5(password));
                        ResultSet result = ps.executeQuery();
                        String departNum = null;
                        while(result.next()){
                            departNum = result.getString("departNum");
                            if (departNum==null){
                                departNum = String.valueOf(0);
                            }
                            this.dispose();
                            new BlueToothTools(departNum);
                        }
                        if (departNum==null){
                            this.ts.setText("请输入正确的账号和密码");
                        }
                    } catch (SQLException | ClassNotFoundException throwables) {
                        this.ts.setText("网络异常，请检查网络！");
                    }finally {
                        try {
                            conn.close();
                        } catch (SQLException ts) {
                            ts.printStackTrace();
                        }
                    }
                    this.ifVerify = false;
                    this.verifyCode=null;
                }else{
                    this.ifVerify = false;
                    this.verifyCode=null;
                    System.out.println(ifVerify+","+verifyString+","+verifyCode);
                    this.ts.setText("验证码错误!请重新获取验证码!");
                }
            }else{
                this.ts.setText("请输入正确的账号和密码!");
            }

        }else{
            this.ts.setText("异常操作!");
        }
    }

    private void getVerify() {
        try{
            // 获取图形验证码
            Map<String, Object> checkCode = CaptchaGenerator.generateCaptcha();
            this.verifyCode = (String) checkCode.get("captchaString");
            this.ifVerify = true;
        }catch (Exception e){
            this.ts.setText("获取验证码异常!");
            e.printStackTrace();
        }
    }
}
